//
//  NetworkManager.swift
//  SwiftForwardWayZhe
//
//  Created by 刘喆 on 2022/1/11.
//

import UIKit
import Moya
import HandyJSON
let NetworkProvider = MoyaProvider<NetworkAPI>()

enum NetworkAPI {
    // 登陆(筛查,质检)
    case login_national(params: NSDictionary)
    // 登陆(医生,校医)
    case sys_Login(params:NSDictionary)
    
}

extension NetworkAPI:TargetType{

    public var baseURL: URL{
        switch self {
        case .login_national:
            return URL(string: "https://www.guozikeji.com/")!
        case .sys_Login:
            return URL(string: "https://www.guozikeji.com/")!
        }
    }
    
    // 对应的不同API path
    var path: String {
        switch self {
        case .login_national: return "newdeparture/sight/phoneLogin"
        case .sys_Login: return"/newdeparture/sight/sysLogin"

        }
    }
    
    // 请求类型
    public var method: Moya.Method {
        
        switch self {
        case .login_national: return .post
        case .sys_Login: return .post
            
        }

    }
    
    // 请求任务事件（这里附带上参数）
    public var task: Task {
        switch self {
        case .login_national(let params):
            
            var parmeters: [String : Any] = [:]
            parmeters["phone"] = params["phone"]
            parmeters["pw"] = params["pw"]
            parmeters["v"] = "iOS:v3.2.10"
            let jsonData:Data! = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
            
            return .requestCompositeData(bodyData: jsonData, urlParameters: parmeters)
            
            
            
            
        case .sys_Login(params: let params):
            
            var parmeters: [String : Any] = [:]
            parmeters["username"] = params["username"]
            return .requestParameters(parameters: parmeters, encoding: URLEncoding.default)
    
            }
    }
        
    
    // 是否执行Alamofire验证
    public var validate: Bool {
        return false
    }
    
    // 这个就是做单元测试模拟的数据，只会在单元测试文件中有作用
    public var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    // 请求头
    public var headers: [String: String]? {
        return ["Content-type": "application/json"]

    }
}

